#include "playscene.h"
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<QMenuBar>
#include<QMainWindow>
#include"mypushbutton.h"
#include<QSoundEffect>
#include<QMediaPlayer>
#include<QLabel>


playscene::playscene(QWidget *parent)
    : QMainWindow{parent}
{

    //添加背景音乐
    QSoundEffect * Sound = new QSoundEffect(this);
    Sound->setSource(QUrl::fromLocalFile(":/res/tom.wav"));
    Sound->setVolume(0.1f);
    Sound->setLoopCount(10);
    Sound->play();
    //稍微流畅一些
    QTimer::singleShot(1000,this,[=](){
        Sound->setVolume(0.2f);
    });
//    QMediaPlayer *music=new QMediaPlayer(this);
//    music->setSource(QUrl::fromLocalFile(":/res/tom.wav"));
//    music->play();


    //切换音效
    QSoundEffect * clickSound = new QSoundEffect(this);
    clickSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    clickSound->setVolume(0.1f);


    initScene();

    m_timer.start();

    QMenuBar*bar=menuBar();
    setMenuBar(bar);

    QMenu*start=bar->addMenu("开始");
    start->setIcon(QIcon(":/res/gamestation.png"));
    //返回按钮
    QAction* back=start->addAction("返回");
    back->setIcon(QIcon(":/res/return.png"));
    connect(back,&QAction::triggered,[=](){
        clickSound->play();

        //点击释放返回信号
        emit this->chooseBack();
    });
    //暂停按钮
    QAction*pause=start->addAction("暂停");
    pause->setIcon(QIcon(":/res/pause.png"));


    connect(pause,&QAction::triggered,[=](){
        clickSound->play();

        if(life>0){//bug修复，人物得活着你才能暂停
            if(isRun){
                m_timer.stop();
                h_timer.stop();
                isRun=false;
            }
            else {
                m_timer.start();
                h_timer.start();
                isRun=true;
            }
        }
    });


    //监听信号
    connect(&m_timer,&QTimer::timeout,[=](){
        //更新障碍物
        objcome();

        //更新坐标
        updatePostion();
        //重新绘制
        update();

        //碰撞检测
        collisionDetec();


    });


    h_timer.start();
    connect(&h_timer,&QTimer::timeout,[=](){

        //左右偏移
        heli.h_x+=x_offset;
        if(heli.h_x>25||heli.h_x<10)x_offset=-x_offset;

        //自动下坠
        heli.h_y+=12;
        if(heli.h_y>350-heli.tom.height())heli.h_y=350-heli.tom.height();
        heli.setPosition(heli.h_x,heli.h_y);
    });


    //显示游戏结束
    QLabel *label=new QLabel;
    label->setParent(this);

    //设置字体
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(30);
    //设置颜色
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    label->setPalette(pe);

    //将字体设置到标签控件
    label->setFont(font);
    label->setText("GAME OVER");

    label->setGeometry(220,150,300,80);
    label->move(220,-150);

    //游戏结束
    connect(this,&playscene::gameover,[=](){
        label->move(220,150);

//        m_timer.stop();
//        h_timer.stop();
        //游戏结束清屏
        for(int i=0;i<10;i++){
            obj[i].is_free3=true;
        }
        Sound->setVolume(0.1f);//声音调小
        QTimer::singleShot(5000,this,[=](){
            m_timer.stop();
            h_timer.stop();
            Sound->stop();
        });

    });



}

void playscene::initScene()
{
    setFixedSize(700,365);
    setWindowTitle("WalterMitty");
    setWindowIcon(QIcon(":/res/title_stu.png"));

    m_timer.setInterval(10);//刷新率

    h_timer.setInterval(150);

    srand((unsigned int)time(NULL));//随机数种子

}

void playscene::updatePostion()
{
    //更新地图坐标
    map.mapPosition();

    //计算障碍物坐标
    for(int i=0;i<10;i++){
        if(obj[i].is_free3==false){
            obj[i].updatePosition();
        }
    }

    //计算爆炸播放图片
    for(int i=0;i<10;i++){
        if(bombs[i].is_free==false){
            bombs[i].updateInfo();
        }
    }

}

void playscene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(map.m1_x,15,map.map1);
    painter.drawPixmap(map.m2_x,15,map.map2);
    painter.drawPixmap(map.m3_x,15,map.map3);

    painter.drawPixmap(heli.h_x,heli.h_y,heli.tom);

    //绘制障碍物
    for(int i=0;i<10;i++){
        if(obj[i].is_free3==false){
            painter.drawPixmap(obj[i].o_x,obj[i].o_y,obj[i].pix);
        }
    }

    //绘制爆炸
    for(int i=0;i<10;i++){
        if(bombs[i].is_free==false){
            painter.drawPixmap(bombs[i].x,bombs[i].y,
                               bombs[i].pixArr[bombs[i].picIndex]);
            //下标是一直在计算的
        }
    }
}

void playscene::keyPressEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Space&&!e->isAutoRepeat()){//屏蔽长按而产生的指令
        h_timer.stop();
        QTimer::singleShot(90,this,[=](){
            heli.h_y-=15;
            if(heli.h_y<15)heli.h_y=15;
            heli.setPosition(heli.h_x,heli.h_y);
            h_timer.start();

        });

    }

}

//障碍物
void playscene::objcome()
{
    recoder++;
    if(recoder<100){
        return;
    }
    recoder=0;

    if(life>0){//游戏结束，甭来了
        while(1){
            int i=rand()%(30)%10;
            if(obj[i].is_free3){
                obj[i].is_free3=false;
                obj[i].o_x=700+obj[i].pix.width();
                break;
            }
        }
    }

}

void playscene::collisionDetec()
{
    //遍历非空闲物体
    for(int i=0;i<10;i++){
        //如果空闲则看下一个
        if(obj[i].is_free3){
            continue;
        }
        //检测直升机障碍物是否相交
        if(obj[i].o_rect.intersects(heli.h_rect)){

           //播放爆炸效果
           for(int k=0;k<10;k++){
               if(bombs[k].is_free){
                   //空闲即播放
                   bombs[k].is_free=false;
                   bombs[k].x=heli.h_x;
                   bombs[k].y=heli.h_y;

                   break;
               }
           }

           if(obj[i].index==4||obj[i].index==5)life--;
           else life-=2;

           if(life<=0){

//                   m_timer.stop();
//                   h_timer.stop();
                emit this->gameover();

                return;
           }
           //为了让游戏结束动画更明显
           //设为非空闲
           obj[i].is_free3=true;



        }
    }
}
